home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / ftpsrvsr / ftpserv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  15.6 KB  |  523 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*    ftpserv.c                                                         */
  4. /*                                                                      */
  5. /*    Durch bloßes Ändern der Extension im Dateinamen, läßt sich Pro-   */
  6. /*    gramm als normale GEM-Anwendung oder aber als Accessory betrei-   */
  7. /*    ben.                                                              */
  8. /*                                                                      */
  9. /*    Copyright (c)  FORTEC/pm 1993                                     */
  10. /*                                                                      */
  11. /************************************************************************/
  12.  
  13. /* -------------------------------------------------------------------- */
  14. /*    Headerdateien einbinden.                                          */
  15. /* -------------------------------------------------------------------- */
  16.  
  17. #include <aes.h>
  18. #include <stdio.h>
  19. #include <tos.h>
  20. #include <vdi.h>
  21. #include <ext.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include "cookie.h"
  26. #include "inetcust.h"
  27. #include "tcpdef.h"
  28. #include "ftp.h"
  29.  
  30. #define LINESIZE 90L
  31. #define NUMLINES 10
  32. #define WINW     600
  33. #define WINH     200
  34.  
  35. /* -------------------------------------------------------------------- */
  36. /*    Extern definierte globale Variablen.                              */
  37. /* -------------------------------------------------------------------- */
  38.                                     /* Mittels dieser Variablen kann    */
  39. extern int _app;                    /* das Programm feststellen, ob es  */
  40.                                     /* als Accessory oder normale App-  */
  41.                                     /* likation gestartet wurde.        */
  42. COOKIE *cookie;
  43. INETCUST *custom;
  44. extern int write_log(char *);  
  45. extern void closetcp(void);
  46.  
  47.  
  48. /* -------------------------------------------------------------------- */
  49. /*    Globale Variablen.                                                */
  50. /* -------------------------------------------------------------------- */
  51.  
  52. int  whandle;                       /* Handle für geöffnetes Fenster.   */
  53. char title[] = "TUW-FTP-Server";    /* Titelzeile des Fensters.         */
  54. int  gl_wchar,                      /* Größe und Breite eines Buchsta-  */
  55.      gl_hchar,                      /* ben (wichtig falls mit unter-    */
  56.      gl_wbox,                       /* schiedlichen Bildschirmauflö-    */
  57.      gl_hbox;                       /* sungen gearbeitet wird) bzw.     */
  58.                                     /* einer Box.                       */
  59. int  phys_handle,                   /* Handles für GEM und VDI.         */
  60.      handle;
  61. int  max_x,                         /* Maximale Größe der Arbeitsfläche */
  62.      max_y;
  63. int  appl_id,                       /* Identifikationsnummer des Prog.  */
  64.      menu_id;                       /* Id.-nummer im Menü 'Desk'.       */
  65.  
  66. char log_text[NUMLINES][LINESIZE+1];
  67. int  line_tab[NUMLINES];
  68. int  actline = 0;
  69. FTP ftp;
  70. int busy = 0;
  71. FILE *fp_pass;
  72.  
  73. void redraw_window( int all );
  74.  
  75. int scroll_log(int actline)
  76. {            /* rotate log lines */
  77. int i;
  78. int line;
  79.  
  80.     if(actline < NUMLINES-1) return(actline+1);
  81.     line = line_tab[0];
  82.     for(i=0; i<NUMLINES-1; i++)
  83.     {
  84.         line_tab[i] = line_tab[i+1];
  85.     }
  86.     line_tab[NUMLINES-1] = line;
  87.     return(NUMLINES-1);
  88. }
  89.  
  90. int log(char *str)
  91. {
  92. int i;
  93. char *s;
  94. int len;
  95.     
  96.     if(!str) return(0);
  97.     len = (int)strlen(str);
  98.     log_text[line_tab[actline]][0] = 0;
  99.     if(!len)
  100.         actline = scroll_log(actline);
  101.     s = log_text[line_tab[actline]];
  102.     while(len > 0)
  103.     {
  104.         for(i=0; i <= LINESIZE; i++)
  105.         {
  106.             if(str[i] >= ' ') *s++ = str[i];
  107.             if(!str[i]) break;
  108.             len--;
  109.         }
  110.         *s=0;
  111.         actline = scroll_log(actline);
  112.         s = log_text[line_tab[actline]];
  113.     }
  114.     *s = 0;
  115.     redraw_window(1);
  116.     return(1);
  117. }
  118.  
  119. /* -------------------------------------------------------------------- */
  120. /*    open_window()                                                     */
  121. /*                                                                      */
  122. /*    Öffnen eines Fensters                                             */
  123. /* -------------------------------------------------------------------- */
  124.  
  125. void open_window( void )
  126. {
  127.    if(whandle <= 0)
  128.    {
  129.       whandle = wind_create( NAME|CLOSER|MOVER, 0, 0, max_x + 1, max_y + 1 );
  130.       if( whandle <= 0 )
  131.          return;
  132.  
  133.       wind_set ( whandle, WF_NAME, title );
  134.       wind_open( whandle, 100, 100, WINW, WINH );
  135.    }
  136.    else
  137.       wind_set( whandle, WF_TOP );
  138. }
  139.  
  140. /* -------------------------------------------------------------------- */
  141. /*    min()                                                             */
  142. /*                                                                      */
  143. /*    Minimum zweier Zahlen berechnen.                                  */
  144. /* -------------------------------------------------------------------- */
  145.  
  146. int min( int a, int b)
  147. {
  148.    if( a > b )
  149.       return( b );
  150.    else
  151.       return( a );
  152. }
  153.  
  154. /* -------------------------------------------------------------------- */
  155. /*    max()                                                             */
  156. /*                                                                      */
  157. /*    Maximum zweier Zahlen bestimmen.                                  */
  158. /* -------------------------------------------------------------------- */
  159.  
  160. int max( int a, int b)
  161. {
  162.    if( a < b )
  163.       return( b );
  164.    else
  165.       return( a );
  166. }
  167.  
  168. /* -------------------------------------------------------------------- */
  169. /*    rc_intersect()                                                    */
  170. /*                                                                      */
  171. /*    Schnittfläche zweier Rechtecke berechnen.                         */
  172. /* -------------------------------------------------------------------- */
  173.  
  174. int rc_intersect(GRECT *r1, GRECT *r2)
  175. {
  176.    int xl, yu, xr, yd;                      /* left, upper, right, down */
  177.  
  178.    xl      = max( r1->g_x, r2->g_x );
  179.    yu      = max( r1->g_y, r2->g_y );
  180.    xr      = min( r1->g_x + r1->g_w, r2->g_x + r2->g_w );
  181.    yd      = min( r1->g_y + r1->g_h, r2->g_y + r2->g_h );
  182.  
  183.    r2->g_x = xl;
  184.    r2->g_y = yu;
  185.    r2->g_w = xr - xl;
  186.    r2->g_h = yd - yu;
  187.  
  188.    return( r2->g_w > 0 && r2->g_h > 0 );
  189. }
  190.  
  191. /* -------------------------------------------------------------------- */
  192. /*    mouse_on()                                                        */
  193. /*                                                                      */
  194. /*    Mauszeiger anschalten.                                            */
  195. /* -------------------------------------------------------------------- */
  196.  
  197. void mouse_on(void)
  198.  
  199. {
  200.    graf_mouse( M_ON, (void *)0 );
  201. }
  202.  
  203. /* -------------------------------------------------------------------- */
  204. /*    mouse_off()                                                       */
  205. /*                                                                      */
  206. /*    Mauszeiger ausschalten.                                           */
  207. /* -------------------------------------------------------------------- */
  208.  
  209. void mouse_off(void)
  210. {
  211.    graf_mouse( M_OFF, (void *)0 );
  212. }
  213.  
  214. /* -------------------------------------------------------------------- */
  215. /*    redraw_window()                                                   */
  216. /*                                                                      */
  217. /*    Fensterinhalt neu zeichnen, nachdem er zuvor aus irgendeinem      */
  218. /*    Grunde zerstört wurde, oder weil das Fenster neu geöffnet wurde.  */
  219. /* -------------------------------------------------------------------- */
  220.  
  221. void redraw_window( int all )
  222. {
  223.    GRECT   box,
  224.            work;
  225.    int     clip[4];
  226.    int     line,dum;
  227.    int         height;
  228.       
  229.    if( whandle <= 0 )                  /* Wenn kein Fenster auf ist,    */
  230.       return;                          /* braucht auch nicht gezeichnet */
  231.                                        /* zu werden.                    */
  232.  
  233.    if(all)
  234.    {
  235.  
  236.       mouse_off();
  237.  
  238.       vsf_color( handle, 0 );                       /* set white fill   */
  239.       vswr_mode( handle, 1 );                       /* set replace mode */
  240.       vst_height( handle, 6, &dum,&dum,&dum,&height);  /* set small font   */
  241.  
  242.       wind_get( whandle, WF_WORKXYWH, &work.g_x, &work.g_y, &work.g_w,
  243.                 &work.g_h );
  244.       wind_get( whandle, WF_FIRSTXYWH, &box.g_x, &box.g_y, &box.g_w,
  245.                 &box.g_h );
  246.       work.g_w = min( work.g_w, max_x - work.g_x + 1 );
  247.       work.g_h = min( work.g_h, max_y - work.g_y + 1 );
  248.       
  249.       while ( box.g_w > 0 && box.g_h > 0 )
  250.       {
  251.          if( rc_intersect( &work, &box ) )
  252.          {
  253.             clip[0] = box.g_x;
  254.             clip[1] = box.g_y;
  255.             clip[2] = box.g_x + box.g_w - 1;
  256.             clip[3] = box.g_y + box.g_h - 1;
  257.  
  258.             vs_clip( handle, 1, clip );
  259.             if( all )
  260.                vr_recfl( handle, clip );
  261.               /* fill rectangle */
  262.             for(line=0;line < NUMLINES; line++)
  263.             {
  264.               v_gtext( handle, work.g_x, work.g_y + 15 + (line*height),log_text[line_tab[line]]);
  265.             }
  266.             
  267.          }
  268.          wind_get( whandle, WF_NEXTXYWH, &box.g_x, &box.g_y, &box.g_w,
  269.                    &box.g_h );
  270.       }
  271.       mouse_on();
  272.    }
  273. }
  274.  
  275. /* -------------------------------------------------------------------- */
  276. /*    handle_message()                                                  */
  277. /*                                                                      */
  278. /*    Auswertung der Ereignisse des Multi-Events bezüglich des Message- */
  279. /*    buffers.                                                          */
  280. /* -------------------------------------------------------------------- */
  281.  
  282. int handle_message( int pipe[8] )
  283. {
  284.    switch ( pipe[0] )
  285.    {
  286.       case WM_REDRAW:
  287.          redraw_window(1);
  288.       break;
  289.  
  290.       case WM_TOPPED:
  291.          wind_set( whandle, WF_TOP );
  292.       break;
  293.  
  294.       case WM_CLOSED:
  295.  
  296.          if(_app)
  297.          {
  298.           closetcp();
  299.           busy = 0;
  300.            }
  301.            if(busy > 0)
  302.            {
  303.                break;
  304.            }
  305.          if(pipe[3] == whandle )
  306.          {
  307.             wind_close( whandle );
  308.             wind_delete( whandle );
  309.             whandle = 0;
  310.          }
  311.          if( _app )
  312.             return(1);
  313.       break;
  314.  
  315.       case WM_MOVED:
  316.       case WM_SIZED:
  317.          if( pipe[3] == whandle )
  318.             wind_set( whandle, WF_CURRXYWH,  pipe[4], pipe[5],
  319.                       pipe[6], pipe[7] );
  320.       break;
  321.  
  322.       case AC_OPEN:
  323.          if( pipe[4] == menu_id )
  324.          {
  325.             open_window();
  326.          }
  327.       break;
  328.  
  329.       case AC_CLOSE:
  330.          if( pipe[3] == menu_id )
  331.             whandle = 0;
  332.       break;
  333.    }
  334.    return(0);
  335. }
  336.  
  337. /* -------------------------------------------------------------------- */
  338. /*    event_loop()                                                      */
  339. /*                                                                      */
  340. /*    Die Multi-Event-Schleife.                                         */
  341. /* -------------------------------------------------------------------- */
  342.  
  343. static void event_loop( void )
  344. {
  345.    int x, y,
  346.        kstate,
  347.        key,
  348.        clicks,
  349.        event,
  350.        state;
  351.    int pipe[8];
  352.    int quit;
  353.    int timer;
  354.  
  355.    quit = 0;
  356.    timer = 100;
  357.    do
  358.    {
  359.       event = evnt_multi( MU_MESAG | MU_TIMER,
  360.                           2, 0x1, 1,
  361.                           0, 0, 0, 0, 0,
  362.                           0, 0, 0, 0, 0,
  363.                           pipe,
  364.                           timer, 0,
  365.                           &x, &y, &state, &kstate, &key, &clicks );
  366.  
  367.  
  368.       if( event & MU_MESAG)
  369.       {
  370.          wind_update(BEG_UPDATE);
  371.          quit = handle_message( pipe );
  372.          wind_update(END_UPDATE);
  373.       }
  374.         if(busy >= 0) switch(ftpserv(&ftp))
  375.         {
  376.             case -1:
  377.                 busy = -1;
  378.                 timer = 1000;
  379.                 break;
  380.             
  381.             case 0:
  382.                 busy = 0;
  383.                 /* do nothing */
  384.                 break;
  385.                 
  386.             case 1:
  387.                 /* connection established */
  388.                 timer = 10;
  389.                 break;
  390.                 
  391.             case 2:
  392.                 /* connection closed */
  393.                 busy = 0;
  394.                 ftp.username[0] = 0;
  395.                 ftp.logged_in = 0;
  396.                 ftp.ftp_ctl = 0;
  397.                 ftp.cmd = IDLE;
  398.                 timer = 500;
  399.                 break;
  400.  
  401.             case 3:
  402.                 /* connection busy */
  403.                 busy = 1;
  404.                 break;
  405.         }
  406.       
  407.  
  408.  
  409.    }
  410.    while (!quit);
  411. }
  412.  
  413. /* -------------------------------------------------------------------- */
  414. /*    main()                                                            */
  415. /*                                                                      */
  416. /*    Kernstück des Programms.                                          */
  417. /* -------------------------------------------------------------------- */
  418.  
  419. int main( void )
  420. {
  421.    int i;
  422.    int work_in[11];
  423.    int work_out[57];
  424.    time_t timer;
  425.    char logstr[80];
  426.    /* ----------------------------------------------------------------- */
  427.    /* Initialization                                                    */
  428.    /* ----------------------------------------------------------------- */
  429.  
  430.    appl_id = appl_init();
  431.    if( appl_id != -1 )
  432.    {
  433.     cookie = get_cookie(INETCUSTCOOKIE);
  434.     if(!cookie || (custom = (INETCUST *)(cookie->val)) == NULL)
  435.     {
  436.         printf("INETCUST not loaded!\n");
  437.         if(_app)
  438.         {
  439.        appl_exit();
  440.          exit(1);
  441.         }
  442.         else while(1) evnt_timer(0,1000);
  443.     }
  444.  
  445.     if((fp_pass = fopen(custom->passwd,"r")) == NULL)
  446.     {
  447.         printf("Cannot find '%s'\n",custom->passwd);
  448.           if(_app)
  449.           {
  450.           appl_exit();
  451.             exit(1);
  452.         }
  453.            else while(1) evnt_timer(0,1000);
  454.     }
  455.     fclose(fp_pass);
  456.  
  457.     ftp.username[0] = 0;
  458.     ftp.logged_in = 0;
  459.     ftp.ftp_ctl = 0;
  460.     ftp.cmd = IDLE;
  461.     busy = 0;
  462.  
  463.     for(i=0; i< NUMLINES; i++)        /* init log table */
  464.     {
  465.         line_tab[i] = i;
  466.          log_text[i][0] = 0;
  467.        }
  468.        actline = 0;
  469.  
  470.       for (i = 0; i < 10; i++)
  471.          work_in[i]  = 1;
  472.          work_in[10] = 2;
  473.          phys_handle = graf_handle( &gl_wchar, &gl_hchar, &gl_wbox,
  474.                                      &gl_hbox );
  475.                                      
  476.       handle = phys_handle;
  477.       v_opnvwk( work_in, &handle, work_out );
  478.       if( handle != 0 )
  479.       {
  480.          max_x = work_out[0];
  481.          max_y = work_out[1];
  482.          if( !_app )
  483.          {
  484.             menu_id = menu_register( appl_id, "  FTP-Server" );
  485.          }
  486.          else
  487.          {
  488.             graf_mouse( 0, (void*)0 );
  489.             open_window();
  490.          }
  491.  
  492.       sprintf(logstr,"FTP-Server %s running",FTP_VERSION);
  493.       log(logstr);
  494.       log(" (c) hw, pm ForTec 1992,1993");
  495.       time(&timer);
  496.       sprintf(logstr,"FTP-Server up at %s\n",ctime(&timer)); 
  497.       write_log(logstr);
  498.  
  499.    /* ----------------------------------------------------------------- */
  500.    /* Event Loop                                                        */
  501.    /* ----------------------------------------------------------------- */
  502.  
  503.          event_loop();
  504.  
  505.    /* ----------------------------------------------------------------- */
  506.    /* Deinitialization                                                  */
  507.    /* ----------------------------------------------------------------- */
  508.  
  509.          ftp.cmd = QUIT;
  510.          ftpserv(&ftp);
  511.          v_clsvwk( handle );
  512.       }
  513.       appl_exit();
  514.    }
  515.    return(0);
  516. }
  517.  
  518.  
  519.  
  520. /* -------------------------------------------------------------------- */
  521. /*    End of FTPSERV.C                                                   */
  522. /* -------------------------------------------------------------------- */
  523.